﻿using System;
using System.Collections.Generic;
using System.Linq;

using PortalIntranet.DatabaseComponents;
using PortalIntranet.DatabaseComponents.WebParts;
using PortalIntranet.ObjectComponents.WebParts;
using PortalIntranet.ObjectComponents.WebParts.LinksGerais;
using PortalIntranet.BusinessComponents.WebParts.LinksGerais;
using PortalIntranet.BusinessComponents.WebParts.Aniversariantes;
using PortalIntranet.BusinessComponents.WebParts.ConteudosHtml;
using PortalIntranet.DatabaseComponents.Administracao;
using PortalIntranet.BusinessComponents.WebParts.Ramais;
using PortalIntranet.BusinessComponents.WebParts.ConteudosWeb;

namespace PortalIntranet.BusinessComponents.WebParts
{
	public class WebPartsBll
	{
		public static WebPart Obter(int id)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.WebParts
					.Where(c => c.Id == id
						&& !c.Desativado
						&& !c.Excluido)
					.SingleOrDefault();
			}
		}

		public static List<WebPart> ListarWebParts(int webpage)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.WebParts
					.Where(c => c.WebPageId == webpage
						&& !c.Desativado
						&& !c.Excluido)
					.OrderBy(o => o.Linha).ThenBy(o => o.Ordem)
					.ToList();
			}
		}

		public static WebPartBaseInfo ConverterWebPart(Departamento departamento, WebPart webpart)
		{
			if (webpart == null) return null;

			switch (webpart.TipoWebPart)
			{
				case TipoWebPart.Aniversariantes:
					return AniversariantesBll.ConverterWebPart(departamento.EmpresaId, webpart);
				case TipoWebPart.Biblioteca:
					break;
				case TipoWebPart.ConteudoHtml:
					return ConteudosHtmlBll.ConverterWebPart(webpart);
				case TipoWebPart.ConteudoWeb:
					return ConteudosWebBll.ConverterWebPart(webpart);
				case TipoWebPart.Customizavel:
					break;
				case TipoWebPart.Enquetes:
					break;
				case TipoWebPart.Eventos:
					break;
				case TipoWebPart.Formularios:
					break;
				case TipoWebPart.ForunsDiscussao:
					break;
				case TipoWebPart.Galerias:
					return GaleriasBll.ConverterWebPart(webpart);
				case TipoWebPart.LinksGerais:
					return LinksGeraisBll.ConverterWebPart(webpart);
				case TipoWebPart.Listas:
					break;
				case TipoWebPart.Noticias:
					break;
				case TipoWebPart.Ramais:
					return RamaisBll.ConverterWebPart(departamento.EmpresaId, webpart);
				case TipoWebPart.Relatorios:
					break;
				case TipoWebPart.Reunioes:
					break;
				case TipoWebPart.TabelasDinamicas:
					break;
				default:
					break;
			}

			return null;
		}

		public static void AtualizarLocalizacao(WebPartLocalizacaoInfo localizacao)
		{
			using (var contexto = new IntranetContexto())
			{
				var webpart = contexto.WebParts.Find(int.Parse(localizacao.Id));

				if (webpart != null)
				{
					webpart.Linha = (LinhaPortlet)int.Parse(localizacao.Linha);
					webpart.Coluna = int.Parse(localizacao.Coluna);
					webpart.Ordem = int.Parse(localizacao.Ordem);
					contexto.SaveChanges();
				}
			}
		}
	}
}
